/*
***************************
*  ytwx common javascript  *
*  dependent jquery-1.4.2 *
*  version:2.0            *
*  date:2011/04/18        *
***************************
*/
if (!window.ytwx) {
	window.ytwx = {};
}
window._ = window.ytwx;
ytwx.dialog = function(title,content,width,height,cssName){
	if($("#floatBoxBg").length<=0){
		var temp_float="<div id=\"floatBoxBg\" style=\"filter:alpha(opacity=30);opacity:0.3;mozopacity:0.3;-moz-opacity:0.3;background-color:#000;position:absolute;top:0;left:0;\"></div>";
			temp_float+="<div id=\"floatBox\" class=\"floatBox\" style=\"position:absolute;top:50px;left:40%;padding:5px;background:#9b9b9b;width:400px;\">";
  				temp_float+="<div class=\"popup\">";
  					temp_float+="<div class=\"popup_tit\">";
  						temp_float+= "<h2 id=\"title\"></h2>";
  						temp_float+= "<span class=\"more\"><a id=\"closea\" href=\"javascript:ytwx.dialog.closebox();\"></a></span>";
  					temp_float+="</div>";
  					temp_float+="<div id=\"content\" class=\"popup_con\"></div>";
  				temp_float+="</div>";
			temp_float+="</div>";
		$("body").append(temp_float);
	}
	$("#floatBox").find('#title').html(title);
	contentType=content.substring(0,content.indexOf(":"));
	content=content.substring(content.indexOf(":")+1,content.length);
	switch(contentType){
  		case "url":
  			content_array=content.split("?");
		  	$.ajax({
				type:content_array[0],
				url:content_array[1],
				data:content_array[2],
				error:function(){
			  		$("#floatBox").find('#content').html("error...");
				},
    			success:function(html){
      				$("#floatBox").find('#content').html(html);
    			}/*,async: false*/
  			});
 			break;
 		case 'datacontent':
 		     $("#floatBox").find('#content').html(content);
 		     break;
  		case "text":
  			$("#floatBox").find('#content').html("<p class=\"p_t1\">"+content+"</p></div>");
 			 break;
  		case "id":
  			$("#floatBox").find('#content').html($("#"+content+"").html());
  			break;
 		case "iframe":
  			$("#floatBox").find('#content').html("<iframe src=\""+content+"\" width=\"100%\" height=\""+(parseInt(height)-30)+"px"+"\" scrolling=\"auto\" frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\"></iframe>");
	}
	$("#floatBox").find(".popup").css({"height":(height=="auto"?185:parseInt(height))-5+"px"});	
	$("#floatBoxBg").show();
	$("#floatBox").attr("class","floatBox "+cssName);
	$("#floatBox").css({"width":width,"height":height});

	ytwx.dialog.closebox = function(){
		$("#floatBoxBg").hide();
		$("#floatBox").find('#content').html("");
		$("#floatBox").hide();
		$(window).unbind("resize");
		$(window).unbind("scroll");
	};
	
	ytwx.dialog.reset = function(width,height){
		var windowHeight = $(window).height();
		var windowWidth = $(window).width();
		var elementHeight = parseInt($('#floatBox').height());
		var elementWidth = parseInt(width);
		var windowTop = $(window).scrollTop();
		var windowLeft = $(window).scrollLeft();
		var elementTop = windowTop + (elementHeight > windowHeight ? windowHeight/10 : (windowHeight - elementHeight)/2);
		var elementLeft = windowLeft + (elementWidth > windowWidth ? 0 : (windowWidth - elementWidth)/2);
		$("#floatBox").css({"left":elementLeft+"px","top":elementTop+"px"});
		$("#floatBoxBg").css({"height":$(document).height()+"px","width":windowWidth+windowLeft+"px"});
	};
	$(window).bind("resize",function(){setTimeout("ytwx.dialog.reset('"+width+"','"+height+"')",50);});
//	$(window).bind("scroll",function(){setTimeout("ytwx.dialog.reset('"+width+"','"+height+"')",150);});
	ytwx.dialog.reset(width,height);
	$("#floatBox").show();
};
function addbox(id,title,content,width,height,cssName){
		if($('#'+id).length>0){return false}
		var temp_float="<div id=\""+id+"\" class=\"floatBox\" style=\"position:absolute;top:50px;left:40%;padding:5px;background:#e8a421;width:400px;\">";
		temp_float+="<div class=\"popup\">";
		temp_float+="<div class=\"popup_tit\">";
		temp_float+= "<h2 class=\"title\"></h2>";
		temp_float+= "<span class=\"more\"><a href=\"#\" onclick=\"closebox(this)\"><img src=\"APP/Tpl/default/Kacount/Public/Images/bt_close.gif\" ></a></span>";
		temp_float+="</div>";
		temp_float+="<div class=\"popup_con\"></div>";
		temp_float+="</div>";
		temp_float+="</div>";
		$("body").append(temp_float)
		$("#"+id).find('.title').html(title);
	contentType=content.substring(0,content.indexOf(":"));
	content=content.substring(content.indexOf(":")+1,content.length);
	switch(contentType){
  		case "url":
  			content_array=content.split("?");
		  	$.ajax({
				type:content_array[0],
				url:content_array[1],
				data:content_array[2],
				error:function(){
			  		$("#"+id).find('.popup_con').html("error...");
				},
    			success:function(html){
      				$("#"+id).find('.popup_con').html(html);
    			}/*,async: false*/
  			});
 			break;
 		case 'datacontent':
 		     $("#"+id).find('.popup_con').html(content);
 		     break;
  		case "text":
  			$("#"+id).find('.popup_con').html("<p class=\"p_t1\">"+content+"</p><div class=\"popup_btn\"><input type=\"button\" class=\"p_btn02\" value=\"确定\" onclick=\"ytwx.dialog.closebox();\"/></div>");
 			 break;
  		case "id":
  			$("#"+id).find('.popup_con').html($("#"+content+"").html());
  			break;
 		case "iframe":
  			$("#"+id).find('.popup_con').html("<iframe src=\""+content+"\" width=\"100%\" height=\""+(parseInt(height)-30)+"px"+"\" scrolling=\"auto\" frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\"></iframe>");
	}
	$("#"+id).find(".popup").css({"width":parseInt(width)+"px","height":(height=="auto"?185:parseInt(height))-5+"px"});	
	$("#"+id).attr("class","floatBox "+cssName);
	$("#"+id).css({"width":width,"height":height});
	
		var windowHeight = $(window).height();
		var windowWidth = $(window).width();
		var elementHeight =parseInt($('#'+id).height());
		var elementWidth = parseInt(width);
		var windowTop = $(window).scrollTop();
		var windowLeft = $(window).scrollLeft();
		var elementTop = windowTop + (elementHeight > windowHeight ? windowHeight/10 : (windowHeight - elementHeight)/2);
		var elementLeft = windowLeft + (elementWidth > windowWidth ? 0 : (windowWidth - elementWidth)/2);
		$("#"+id).css({"left":elementLeft+"px","top":elementTop+"px"});
}
function closebox(objId){
		$(objId).parents('.floatBox').remove();
		return false;
}

